#include "messagemodel.h"

MessageModel::MessageModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

int MessageModel::rowCount(const QModelIndex &parent) const
{
    return m_contacts.count();
}

QVariant MessageModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < rowCount())
        switch (role) {
        case MessageRole: return m_contacts.at(index.row()).message;break;
        case SendernameRole: return m_contacts.at(index.row()).sendername;break;
        case SenderimgRole: return m_contacts.at(index.row()).senderimg;break;
        case SenderidRole: return m_contacts.at(index.row()).senderid;break;
        case TimeRole:return m_contacts.at(index.row()).time;break;
        case MsgTypeRole:return m_contacts.at(index.row()).msgtype;break;
        default: return QVariant();
        }

    // FIXME: Implement me!
    return QVariant();
}

QHash<int, QByteArray> MessageModel::roleNames() const
{
    static const QHash<int, QByteArray> roles {
        {MessageRole , "message"},
        {SendernameRole,"sendername"},
        {SenderimgRole,"senderimg"},
        {SenderidRole,"senderid"},
        {TimeRole,"time"},
        {MsgTypeRole,"msgtype"}
    };
    return roles;
}

QVariantMap MessageModel::get(int row) const
{
    const Contact contact = m_contacts.value(row);
    return {
        {"message", contact.message},
        {"sendername", contact.sendername},
        {"senderimg", contact.senderimg},
        {"senderid", contact.senderid},
        {"time", contact.time},
        {"msgtype",contact.msgtype}
    };
}

void MessageModel::append(const Contact contact)
{
    int row = 0;
    while (row < m_contacts.count())
        ++row;

    beginInsertRows(QModelIndex(), row, row);
    m_contacts.insert(row,contact);
    endInsertRows();
}

